# -*- encoding=UTF-8 -*-
from photoShare import db
from random import randint
from _datetime import datetime


class User(db.Model):
  uid = db.Column(db.Integer, primary_key=True, autoincrement=True)
  username = db.Column(db.String(80), unique=True)
  password = db.Column(db.String(32))
  head_url = db.Column(db.String(256))
  images = db.relationship('Image', backref='user', lazy=True)

  def __init__(self, username, password):
    self.username = username
    self.password = password
    self.head_url = 'https://images.nowcoder.com/head/' + str(randint(0, 1000)) + 'm.png'

  def __repr__(self):
    return '<User %d %s>' % (self.uid, self.username)


class Image(db.Model):
  iid = db.Column(db.Integer, primary_key=True, autoincrement=True)
  user_id = db.Column(db.Integer, db.ForeignKey('user.uid'))
  url = db.Column(db.String(512))
  created_date = db.Column(db.DateTime)
  comments = db.relationship('Comment')

  def __init__(self, url, user_id):
    self.url = url
    self.user_id = user_id
    self.created_date = datetime.now()

  def __repr__(self):
    return '<Image %d %s>' % (self.iid, self.url)


class Comment(db.Model):
  cid = db.Column(db.Integer, primary_key=True, autoincrement=True)
  content = db.Column(db.String(512))
  image_id = db.Column(db.Integer, db.ForeignKey('image.iid'))
  user_id = db.Column(db.Integer, db.ForeignKey('user.uid'))
  status = db.Column(db.Integer, default=0)
  user = db.relationship('User')

  def __init__(self, content, image_id, user_id):
    self.content = content
    self.image_id = image_id
    self.user_id = user_id

  def __repr__(self):
    return '<Comment %d %s>' % (self.cid, self.content)
